package com.ustb.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ustb.springboot.common.utils.R;
import com.ustb.springboot.common.utils.TokenUtil;
import com.ustb.springboot.entity.QuestionDatetimeEntity;
import com.ustb.springboot.service.OptionService;
import com.ustb.springboot.service.QuestionDatetimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ustb.springboot.dao.AnswerDatetimeDao;
import com.ustb.springboot.entity.AnswerDatetimeEntity;
import com.ustb.springboot.service.AnswerDatetimeService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service("answerDatetimeService")
public class AnswerDatetimeServiceImpl extends ServiceImpl<AnswerDatetimeDao, AnswerDatetimeEntity> implements AnswerDatetimeService {

    @Resource
    private AnswerDatetimeDao answerDatetimeDao;
    @Autowired
    private QuestionDatetimeService questionDatetimeService;
    @Autowired
    private OptionService optionService;


    @Override
    public R save(HttpServletRequest request, Map<String, Object> body) {
        if (null == body) return R.error("参数错误");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        List<String> beginAndEnd = (ArrayList) body.get("beginAndEnd");
        Date beginTime = null, endTime = null, creteTime = null;
        try {
            beginTime = (sdf.parse(beginAndEnd.get(0)));
            endTime = (sdf.parse(beginAndEnd.get(1)));
            creteTime = sdf.parse(body.get("createTime").toString());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //日期时间处理


        AnswerDatetimeEntity answerDatetime = new AnswerDatetimeEntity()
                .setId(body.get("id").toString())
                .setFormId(body.get("formId").toString())
                .setQuestionId(body.get("questionId").toString())
                .setUserId(TokenUtil.getId(request))
                .setBeginTime(beginTime)
                .setEndTime(endTime)
                .setCreateTime(creteTime)
                .setIsSubmit((Boolean) body.get("isSubmit"));

        return this.saveOrUpdate(answerDatetime) ? R.ok(20000, "保存成功") : R.error(2000, "保存失败");
//        if (answerDatetimeDao.exists(new QueryWrapper<AnswerDatetimeEntity>().eq("id", answerDatetime.getId())))
//            return this.updateById(answerDatetime) ? R.ok(20000, "保存成功") : R.error(2000, "保存失败");
//
//        return this.save(answerDatetime) ? R.ok(20000, "保存日期时间成功") : R.error(2000, "保存日期时间失败");
    }

    @Override
    public R delete(Map<String, Object> body) {
        QueryWrapper<AnswerDatetimeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        return answerDatetimeDao.delete(queryWrapper) == 1 ? R.ok(20000, "删除成功！") : R.error(2000, "删除失败");
    }

    @Override
    public List<AnswerDatetimeEntity> queryAnsDateTimeWithQuestionId(String questionId) {
        QueryWrapper<AnswerDatetimeEntity>answerDatetimeEntityQueryWrapper = new QueryWrapper<>();
        answerDatetimeEntityQueryWrapper.eq("question_id",questionId);
        List<AnswerDatetimeEntity> answerDatetimeEntityList = answerDatetimeDao.selectList(answerDatetimeEntityQueryWrapper);
        return answerDatetimeEntityList;
    }

    @Override
    public Map<String, Object> summerizeDatetimeMessage(Map<String, Object> body) {
        List<AnswerDatetimeEntity> answerDatetimeEntityList = new ArrayList<>();
        answerDatetimeEntityList = queryAnsDateTimeWithQuestionId(body.get("question_id").toString());
        List<QuestionDatetimeEntity> questionDatetimeEntityList = questionDatetimeService.queryQuestionDatetimeEntity(body);
        Map<String , Object> summerizeMessage = new HashMap<>();
        List<Object> totalTime = new ArrayList<>();
        Integer sum = 0;
        for(AnswerDatetimeEntity answerDatetimeEntity : answerDatetimeEntityList){
            List<Object> beginAndEndTime = new  ArrayList<>();
            beginAndEndTime.add(answerDatetimeEntity.getId());
            beginAndEndTime.add(answerDatetimeEntity.getBeginTime());
            beginAndEndTime.add(answerDatetimeEntity.getEndTime());
            totalTime.add(beginAndEndTime);
            sum ++;
        }
        summerizeMessage.put("question_info",questionDatetimeEntityList.get(0).getQuestionInfo());
        summerizeMessage.put("question_name",questionDatetimeEntityList.get(0).getQuestionName());
        summerizeMessage.put("question_type",questionDatetimeEntityList.get(0).getQuestionType());
        summerizeMessage.put("begin_and_end_times",totalTime);
        summerizeMessage.put("sum",sum);
        return summerizeMessage;
    }

}